package com.gidas.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.gidas.client.GetStopService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;


import com.gidas.client.StopsSerializable;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GetStopServiceImpl extends RemoteServiceServlet implements
	GetStopService {

	
	
	public List<StopsSerializable> getStopService() {
		// TODO Auto-generated method stub
		System.out.println("GetStopServiceImpl...");
		PersistenceManager pm = PMF.get().getPersistenceManager();
        

        List<StopsSerializable> stopsTransport= new ArrayList<StopsSerializable>();

        
        try
        {
        	String query = "select from " + StopDB.class.getName();
            List<StopDB> stopsList = (List<StopDB>) pm.newQuery(query).execute();
        	System.out.println("newQuery(query).execute...");
        	for (StopDB s : stopsList)
        	{
        		if(s.getNativeId()!=null)
        		{
        			StopsSerializable newStop = new StopsSerializable();
        			
        			newStop.setNativeId(s.getNativeId());
        			newStop.setName(s.getName());
        			newStop.setStreet(s.getStreet());
        			newStop.setLat(s.getLat());
        			newStop.setLng(s.getLng());
        			newStop.setCity(s.getCity());
        			newStop.setStartDate(s.getStartDate());
        			
        			//System.out.println(newStop.getName());
        			stopsTransport.add(newStop);
        			//list += s.getName() +" ";
        		}
        	}
        }
        catch(Exception e)
        {
        	System.out.println(e);
        }
        finally
        {        	
        	pm.close();
        	System.out.println("pm.close()...");
        	
        }
       
		return stopsTransport;
	}
	
}
